package com.iplant.pdm.serviceImpl.dao.fpc;

import com.alibaba.fastjson.JSON;
import com.iplant.base.po.enums.DBSchemas;
import com.iplant.base.utils.annotation.dao.DBBaseDAO;
import com.iplant.pdm.service.po.bop.FPCRoutePartPoint;
import com.iplant.pdm.service.po.bop.FPCRouteStation;
import com.iplant.pdm.service.po.bop.FPCStepSOP;
import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.pdm.service.po.bop.partpoint.FPCRouteFrockTool;
import com.iplant.pdm.service.po.bop.partpoint.FPCRouteMeasureTool;
import com.iplant.pdm.service.po.mss.MSSBOMItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;

@DBBaseDAO(FPCRoutePartPoint.class)
public class FPCRoutePartPointDAO extends BaseDAO {
    private static FPCRoutePartPointDAO Instance = null;

    private static Logger logger = LoggerFactory.getLogger(FPCRoutePartPointDAO.class);

    private FPCRoutePartPointDAO() {
        super("");
        this.Init();
    }

    public static FPCRoutePartPointDAO getInstance() {
        if (Instance == null)
            Instance = new FPCRoutePartPointDAO();
        return Instance;
    }


    public List<FPCRoutePartPoint> FPC_QueryRoutePartPointList(BMSEmployee wLoginUser, int wRouteID,
                                                               Pagination wPagination
            , OutResult<Integer> wErrorCode) {
        Map<String, Object> wParams = new HashMap<>();
        wParams.put("RouteID", wRouteID);

        return this.SelectPOList(wLoginUser, wParams, wPagination, wErrorCode);
    }


    /**
     * 自身除开的最大版本号
     *
     * @param wLoginUser
     * @param wName
     * @param wErrorCode
     * @return
     */
    private int FPC_GetMaxVersion(BMSEmployee wLoginUser, String wName, OutResult<Integer> wErrorCode) {

        int wMaxVersion = 0;
        wErrorCode.set(0);

        try {

            String wSQLText = StringUtils.Format(
                    "select Max(SUBSTRING_INDEX(t.Code,CONCAT(t.Name,''.V_''),-1)+0 ) as ItemCount ,t.Name"
                            + "  from  {0}.fpc_routepartpoint t where locate(t.Name,t.Code)=1 and t.Name=:Name group " +
                            "by Name ;",
                    super.getDBName());

            Map<String, Object> wParams = new HashMap<String, Object>();
            wParams.put("Name", wName);

            List<Map<String, Object>> wQueryResultList = super.queryForList(wSQLText, wParams);

            for (Map<String, Object> wSqlDataReader : wQueryResultList) {
                wMaxVersion = StringUtils.parseInt(wSqlDataReader.get("ItemCount"));
            }

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());

            logger.error("FPC_UpdateRoute", ex);
        }
        return wMaxVersion;
    }

    public void FPC_UpdateRoutePartPoint(BMSEmployee wLoginUser, FPCRoutePartPoint wRoutePartPoint,
                                         OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);

        try {
            if (wRoutePartPoint == null || StringUtils.isEmpty(wRoutePartPoint.Name)) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return;
            }
            FPCRoutePartPoint wRoutePartPointO = this.FPC_CheckRoutePartPoint(wLoginUser, wRoutePartPoint, wErrorCode);
            if (wRoutePartPointO.ID > 0) {
                wErrorCode.set(ExceptionTypes.Duplication.getValue());
            }
            if (wErrorCode.Result != 0)
                return;

            if (wRoutePartPoint.ID <= 0 || !wRoutePartPoint.Code.startsWith(wRoutePartPoint.Name)) {

                int wVersion = this.FPC_GetMaxVersion(wLoginUser, wRoutePartPoint.Name, wErrorCode);

                wRoutePartPoint.Code = StringUtils.Format("{0}.V_{1}", wRoutePartPoint.Name,
                        String.format("%04d", wVersion + 1));
            }

            Map<String, Object> wParams = new HashMap<String, Object>();

            wParams.put("Version", wRoutePartPoint.Version);
            wParams.put("RoutePartID", wRoutePartPoint.RoutePartID);

            wParams.put("PartPointCode", wRoutePartPoint.PartPointCode);

            wParams.put("PartPointType", wRoutePartPoint.PartPointType);
            wParams.put("PrevPartPointID", wRoutePartPoint.PrevPartPointID);
            wParams.put("PartPointFile", wRoutePartPoint.PartPointFile);

            wParams.put("StandardPeriod", wRoutePartPoint.StandardPeriod);
            wParams.put("ActualPeriod", wRoutePartPoint.ActualPeriod);
            wParams.put("DefaultOrder", wRoutePartPoint.DefaultOrder);
            wParams.put("OrderID", wRoutePartPoint.OrderID);
            wParams.put("ChangeControl", wRoutePartPoint.ChangeControl);
            wParams.put("ControlPartPointID", StringUtils.Join(",", wRoutePartPoint.ControlPartPointID));
            wParams.put("NextPartPointID", StringUtils.Join(",", wRoutePartPoint.NextPartPointID));
            wParams.put("NextPartPointIDMap", JSON.toJSONString(wRoutePartPoint.NextPartPointIDMap));

            wParams.put("Name", wRoutePartPoint.Name);
            wParams.put("Code", wRoutePartPoint.Code);
            wParams.put("Description", wRoutePartPoint.Description);

            wParams.put("EditorID", wLoginUser.getID());
            wParams.put("EditTime", Calendar.getInstance());
            wParams.put("Active", wRoutePartPoint.Active);

            if (wRoutePartPoint.ID > 0) {
                wParams.put("ID", wRoutePartPoint.ID);
                super.Update(super.getTableName("fpc_routepartpoint"), "ID", wParams);
            } else {
                wParams.put("CreatorID", wLoginUser.getID());
                wParams.put("CreateTime", Calendar.getInstance());
                wRoutePartPoint.ID = super.Insert(super.getTableName("fpc_routepartpoint"), wParams);
            }

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("FPC_UpdateRoutePartPoint", ex);
        }
    }

    public void FPC_DeleteRoutePartPoint(BMSEmployee wLoginUser, int wID, OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);

        try {
            if (wID <= 0) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return;
            }

            Map<String, Object> wParams = new HashMap<String, Object>();

            wParams.put("ID", wID);

            super.Delete(super.getTableName("fpc_routepartpoint"), wParams, " AND ID>0 AND Active=0");

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("FPC_DeleteRoutePartPoint", ex);
        }
    }

    public void FPC_ActiveRoutePartPoint(BMSEmployee wLoginUser, FPCRoutePartPoint wRoutePartPoint, int wActive,
                                         OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);
        // 判断客户信息是否存在(中国：统一社会信用代码，国外:提醒是否有重复）

        try {
            if (wRoutePartPoint == null || wRoutePartPoint.ID <= 0) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return;
            }

            Map<String, Object> wParams = new HashMap<String, Object>();

            wParams.put("ID", wRoutePartPoint.ID);
            wParams.put("EditorID", wLoginUser.getID());
            wParams.put("EditTime", Calendar.getInstance());
            wParams.put("Active", wActive);
            super.Update(super.getTableName("fpc_routepartpoint"), "ID", wParams);

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());

            logger.error("FPC_ActiveRoutePartPoint", ex);
        }
    }

    public List<FPCRoutePartPoint> FPC_QueryRoutePartPointList(BMSEmployee wLoginUser, int wRouteID,
                                                               List<Integer> wRoutePartID, String wRoutePartName,
                                                               String wPartCode, String wName, String wPartPointCode,
                                                               String wNameLike, int wActive, int wHasItem,
                                                               Pagination wPagination, OutResult<Integer> wErrorCode) {
        return this.FPC_QueryRoutePartPointList(wLoginUser, -1, "", wRouteID, wRoutePartID, wRoutePartName, wPartCode,
                wName, wPartPointCode, wNameLike, wActive, wHasItem, wPagination, wErrorCode);
    }


    @SuppressWarnings("unchecked")
    private List<FPCRoutePartPoint> FPC_QueryRoutePartPointList(BMSEmployee wLoginUser, int wID, String wCode,
                                                                int wRouteID, List<Integer> wRoutePartID,
                                                                String wRoutePartName, String wPartCode, String wName,
                                                                String wPartPointCode, String wNameLike, int wActive,
                                                                int wHasItem, Pagination wPagination,
                                                                OutResult<Integer> wErrorCode) {
        List<FPCRoutePartPoint> wRoutePartPointList = new ArrayList<FPCRoutePartPoint>();
        wErrorCode.set(0);

        try {

            // Step02:查询
            Map<String, Object> wParams = new HashMap<String, Object>();
            String wSQLText = "";
            wSQLText = StringUtils.Format("Select t.*,t1.Name as RoutePartName,t1.Code as  RoutePartCode,"
                            + " t1.Version as RoutePartVersion,t1.RouteID,t1.PartCode, t2.Name as RouteName,"
                            + " t2.Code as RouteCode,t2.Version as RouteVersion, t3.Name as CreatorName ,"
                            + " t4.Name as EditorName, t5.Name as PrevPartPointName, group_concat(distinct t6.Name) " +
                            "as NextPartPointName,"
                            + " group_concat(distinct t7.Name) as ControlPartPointName, count(t8.ID) as ItemCount,"
                            + " group_concat(distinct t9.StationID) as StationID, group_concat(distinct t10.Name) as " +
                            "StationName,"
                            + " group_concat(distinct t10.Code) as StationCode from {0}.fpc_routepartpoint t  "
                            + " left join {0}.fpc_routepart t1 on t.RoutePartID=t1.ID "
                            + " left join {0}.fpc_route t2 on t1.RouteID=t2.ID "
                            + " left join {0}.mbs_user t3 on t.CreatorID=t3.ID "
                            + " left join {0}.mbs_user t4 on t.EditorID=t4.ID "
                            + " left join {0}.fpc_routepartpoint t5 on t.PrevPartPointID=t5.ID "
                            + " left join {0}.fpc_routepartpoint t6 on find_in_set(t6.ID,t.NextPartPointID) "
                            + " left join {0}.fpc_routepartpoint t7 on find_in_set(t7.ID,t.ControlPartPointID) "
                            + " left join {0}.fpc_stepsop t8 on t.ID=t8.RoutePartPointID"
                            + " left join {0}.fpc_routestation t9 on t.ID=t9.RoutePartPointID "
                            + " left join {0}.fmc_station t10 on t9.StationID=t10.ID "
                            + " where (:ID<=0 or  t.ID=:ID) and (:Code =''''  or  t.Code=:Code)"
                            + " and (:Name =''''  or  t.Name=:Name) and (:RouteID<=0 or  t1.RouteID=:RouteID)"
                            + " and (:PartCode =''''  or  t1.PartCode=:PartCode) "
                            + " and (:RoutePartName =''''  or  t1.Name=:RoutePartName) "
                            + " and (:PartPointCode =''''  or  t.PartPointCode=:PartPointCode) "
                            + " AND (:Active<0 or  t.Active=:Active) AND (:NameLike = '''' or  t.Name like :NameLike)  "
                            + " AND (:RoutePartID = '''' or  t.RoutePartID IN ({1}) ) group by t.ID ",
                    super.getDBName(), wRoutePartID.size() > 0 ? StringUtils.Join(",", wRoutePartID) : "0");

            wSQLText = this.DMLChange(wSQLText);

            wParams.put("ID", wID);
            wParams.put("Code", wCode);
            wParams.put("Name", wName);
            wParams.put("RouteID", wRouteID);
            wParams.put("PartCode", wPartCode);
            wParams.put("RoutePartName", wRoutePartName);
            wParams.put("PartPointCode", wPartPointCode);
            wParams.put("Active", wActive);
            wParams.put("NameLike", StringUtils.isEmpty(wNameLike) ? "" : ("%" + wNameLike + "%"));
            wParams.put("RoutePartID", StringUtils.Join(",", wRoutePartID));

            List<Map<String, Object>> wQueryResultList = super.queryForList(wSQLText, wParams, wPagination);

            FPCRoutePartPoint wRoutePartPoint = null;
            List<Integer> wRoutePartPointID = new ArrayList<Integer>();
            for (Map<String, Object> wSqlDataReader : wQueryResultList) {
                wRoutePartPoint = new FPCRoutePartPoint();
                wRoutePartPoint.ID = StringUtils.parseInt(wSqlDataReader.get("ID"));
                wRoutePartPoint.Code = StringUtils.parseString(wSqlDataReader.get("Code"));
                wRoutePartPoint.Name = StringUtils.parseString(wSqlDataReader.get("Name"));
                wRoutePartPoint.Version = StringUtils.parseString(wSqlDataReader.get("Version"));
                wRoutePartPoint.RoutePartID = StringUtils.parseInt(wSqlDataReader.get("RoutePartID"));
                wRoutePartPoint.RoutePartCode = StringUtils.parseString(wSqlDataReader.get("RoutePartCode"));
                wRoutePartPoint.RoutePartName = StringUtils.parseString(wSqlDataReader.get("RoutePartName"));
                wRoutePartPoint.RoutePartVersion = StringUtils.parseString(wSqlDataReader.get("RoutePartVersion"));
                wRoutePartPoint.RouteID = StringUtils.parseInt(wSqlDataReader.get("RouteID"));
                wRoutePartPoint.RouteName = StringUtils.parseString(wSqlDataReader.get("RouteName"));
                wRoutePartPoint.RouteCode = StringUtils.parseString(wSqlDataReader.get("RouteCode"));
                wRoutePartPoint.RouteVersion = StringUtils.parseString(wSqlDataReader.get("RouteVersion"));
                wRoutePartPoint.PartCode = StringUtils.parseString(wSqlDataReader.get("PartCode"));
                wRoutePartPoint.PartPointCode = StringUtils.parseString(wSqlDataReader.get("PartPointCode"));
                wRoutePartPoint.PartPointType = StringUtils.parseInt(wSqlDataReader.get("PartPointType"));
                wRoutePartPoint.OrderID = StringUtils.parseInt(wSqlDataReader.get("OrderID"));
                wRoutePartPoint.DefaultOrder = StringUtils.parseString(wSqlDataReader.get("DefaultOrder"));
                wRoutePartPoint.PrevPartPointID = StringUtils.parseInt(wSqlDataReader.get("PrevPartPointID"));
                wRoutePartPoint.PrevPartPointName = StringUtils.parseString(wSqlDataReader.get("PrevPartPointName"));
                wRoutePartPoint.ChangeControl = StringUtils.parseInt(wSqlDataReader.get("ChangeControl"));
                wRoutePartPoint.ControlPartPointID = StringUtils.parseIntList(wSqlDataReader.get("ControlPartPointID"),
                        ",");
                wRoutePartPoint.ControlPartPointName = StringUtils
                        .parseString(wSqlDataReader.get("ControlPartPointName"));
                wRoutePartPoint.StandardPeriod = StringUtils.parseDouble(wSqlDataReader.get("StandardPeriod"));
                wRoutePartPoint.ActualPeriod = StringUtils.parseDouble(wSqlDataReader.get("ActualPeriod"));
                wRoutePartPoint.ItemCount = StringUtils.parseInt(wSqlDataReader.get("ItemCount"));
                wRoutePartPoint.PartPointFile = StringUtils.parseString(wSqlDataReader.get("PartPointFile"));

                wRoutePartPoint.NextPartPointID = StringUtils.parseIntList(wSqlDataReader.get("NextPartPointID"), ",");
                wRoutePartPoint.NextPartPointName = StringUtils.parseString(wSqlDataReader.get("NextPartPointName"));

                wRoutePartPoint.NextPartPointIDMap = JSON
                        .parseObject(StringUtils.parseString(wSqlDataReader.get("NextPartPointIDMap")), Map.class);

                wRoutePartPoint.StationID = StringUtils.parseIntList(wSqlDataReader.get("StationID"), ",");
                wRoutePartPoint.StationName = StringUtils.parseString(wSqlDataReader.get("StationName"));
                wRoutePartPoint.StationCode = StringUtils.parseString(wSqlDataReader.get("StationCode"));

                wRoutePartPoint.Active = StringUtils.parseInt(wSqlDataReader.get("Active"));
                wRoutePartPoint.CreatorID = StringUtils.parseInt(wSqlDataReader.get("CreatorID"));
                wRoutePartPoint.EditorID = StringUtils.parseInt(wSqlDataReader.get("EditorID"));
                wRoutePartPoint.CreatorName = StringUtils.parseString(wSqlDataReader.get("CreatorName"));
                wRoutePartPoint.EditorName = StringUtils.parseString(wSqlDataReader.get("EditorName"));
                wRoutePartPoint.CreateTime = StringUtils.parseCalendar(wSqlDataReader.get("CreateTime"));
                wRoutePartPoint.EditTime = StringUtils.parseCalendar(wSqlDataReader.get("EditTime"));
                wRoutePartPoint.Description = StringUtils.parseString(wSqlDataReader.get("Description"));

                wRoutePartPointList.add(wRoutePartPoint);

                if (!wRoutePartPointID.contains(wRoutePartPoint.ID))
                    wRoutePartPointID.add(wRoutePartPoint.ID);
            }

            if (wHasItem == 1) {

                List<FPCStepSOP> wStepSOPList = FPCStepSOPDAO.getInstance().FPC_QueryStepSOPList(wLoginUser, -1, -1,
                        wRoutePartPointID, "", "", "", "", "", -1, Pagination.getMaxSize(), wErrorCode);

                Map<Integer, List<FPCStepSOP>> wStepSOPDic = null;
                if (wStepSOPList != null && wStepSOPList.size() > 0) {
                    wStepSOPDic = wStepSOPList.stream().collect(Collectors.groupingBy(p -> p.RoutePartPointID));

                }

                List<FPCRouteStation> wRouteStationList = FPCRouteStationDAO.getInstance().FPC_QueryRouteStationList(
                        wLoginUser, -1, "", -1, -1, wRoutePartPointID, "", "", "", "", "", -1, wPagination, wErrorCode);

                Map<Integer, List<FPCRouteStation>> RouteStationDic = null;
                if (wRouteStationList != null && wRouteStationList.size() > 0) {
                    RouteStationDic = wRouteStationList.stream()
                            .collect(Collectors.groupingBy(p -> p.RoutePartPointID));

                }
                if (wStepSOPDic == null && RouteStationDic == null)
                    return wRoutePartPointList;

                for (FPCRoutePartPoint fpcRoutePartPoint : wRoutePartPointList) {
                    if (fpcRoutePartPoint == null || fpcRoutePartPoint.ID <= 0)
                        continue;
                    if (wStepSOPDic != null && wStepSOPDic.containsKey(fpcRoutePartPoint.ID)) {
                        fpcRoutePartPoint.StepSOPList = wStepSOPDic.get(fpcRoutePartPoint.ID);
                    }
                    if (RouteStationDic != null && RouteStationDic.containsKey(fpcRoutePartPoint.ID)) {
                        fpcRoutePartPoint.RouteStationList = RouteStationDic.get(fpcRoutePartPoint.ID);
                    }

                }
            }
            // Step03:配置系统
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("FPC_QueryRoutePartPointList", ex);
        }
        return wRoutePartPointList;
    }

    public FPCRoutePartPoint FPC_QueryRoutePartPoint(BMSEmployee wLoginUser, int wID, String wCode,
                                                     OutResult<Integer> wErrorCode) {
        FPCRoutePartPoint wResult = new FPCRoutePartPoint();
        wErrorCode.set(0);

        try {
            List<FPCRoutePartPoint> wList = null;
            if (wID > 0) {
                wList = this.FPC_QueryRoutePartPointList(wLoginUser, wID, "", -1, null, "", "", "", "", "", -1, 1,
                        Pagination.getDefault(), wErrorCode);

            } else if (StringUtils.isNotEmpty(wCode)) {
                wList = this.FPC_QueryRoutePartPointList(wLoginUser, -1, wCode, -1, null, "", "", "", "", "", - -1, 1,
                        Pagination.getDefault(), wErrorCode);
            }
            if (wList != null && wList.size() > 0) {
                wResult = wList.get(0);
            }
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("FPC_QueryRoutePartPointByID", ex);
        }
        return wResult;
    }

    public FPCRoutePartPoint FPC_QueryRoutePartPoint(BMSEmployee wLoginUser, int wRoutePartID, String wName,
                                                     String wPartPointCode, OutResult<Integer> wErrorCode) {
        FPCRoutePartPoint wResult = new FPCRoutePartPoint();
        wErrorCode.set(0);

        try {
            if (wRoutePartID <= 0)
                return wResult;
            List<FPCRoutePartPoint> wList = null;
            if (StringUtils.isNotEmpty(wName)) {
                wList = this.FPC_QueryRoutePartPointList(wLoginUser, -1, "", -1,
                        StringUtils.parseListArgs(wRoutePartID), "", "", wName, "", "", -1, 1, Pagination.getDefault(),
                        wErrorCode);

            } else if (StringUtils.isNotEmpty(wPartPointCode)) {
                wList = this.FPC_QueryRoutePartPointList(wLoginUser, -1, "", -1,
                        StringUtils.parseListArgs(wRoutePartID), "", "", "", wPartPointCode, "", -1, 1,
                        Pagination.getDefault(), wErrorCode);
            }
            if (wList != null && wList.size() > 0) {
                wResult = wList.get(0);
            }
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("FPC_QueryRoutePartPointByID", ex);
        }
        return wResult;
    }

    public FPCRoutePartPoint FPC_QueryRoutePartPoint(BMSEmployee wLoginUser, int wRouteID, String wPartCode,
                                                     String wRoutePartName, String wName, String wPartPointCode,
                                                     OutResult<Integer> wErrorCode) {
        FPCRoutePartPoint wResult = new FPCRoutePartPoint();
        wErrorCode.set(0);

        try {
            if (wRouteID <= 0 || (StringUtils.isNotEmpty(wRoutePartName) && StringUtils.isNotEmpty(wPartCode)))
                return wResult;

            List<FPCRoutePartPoint> wList = null;
            if (StringUtils.isNotEmpty(wName)) {
                wList = this.FPC_QueryRoutePartPointList(wLoginUser, -1, "", wRouteID, null, wRoutePartName, wPartCode,
                        wName, "", "", -1, 1, Pagination.getDefault(), wErrorCode);

            } else if (StringUtils.isNotEmpty(wPartPointCode)) {
                wList = this.FPC_QueryRoutePartPointList(wLoginUser, -1, "", wRouteID, null, wRoutePartName, wPartCode,
                        "", wPartPointCode, "", -1, 1, Pagination.getDefault(), wErrorCode);
            }
            if (wList != null && wList.size() > 0) {
                wResult = wList.get(0);
            }
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("FPC_QueryRoutePartPointByID", ex);
        }
        return wResult;
    }

    public FPCRoutePartPoint FPC_CheckRoutePartPoint(BMSEmployee wLoginUser, FPCRoutePartPoint wFPCRoutePartPoint,
                                                     OutResult<Integer> wErrorCode) {
        FPCRoutePartPoint wRoutePartPoint = new FPCRoutePartPoint();
        wErrorCode.set(0);

        try {

            // Step0:查询
            Map<String, Object> wParams = new HashMap<String, Object>();
            String wSQLText = StringUtils.Format(
                    "Select * from {0}.fpc_routepartpoint where ID!=:ID "
                            + " and ( Code=:Code or (RoutePartID=:RoutePartID and  ( Name =:Name or "
                            + " (:PartPointCode !='''' and  PartPointCode=:PartPointCode)  ))  ) ",
                    super.getDBName());
            wParams.clear();
            wParams.put("ID", wFPCRoutePartPoint.ID);
            wParams.put("Name", wFPCRoutePartPoint.Name);
            wParams.put("Code", wFPCRoutePartPoint.Code);
            wParams.put("RoutePartID", wFPCRoutePartPoint.RoutePartID);
            wParams.put("PartPointCode", wFPCRoutePartPoint.PartPointCode);

            wSQLText = this.DMLChange(wSQLText);
            List<Map<String, Object>> wQueryResultList = super.queryForList(wSQLText, wParams);
            for (Map<String, Object> wSqlDataReader : wQueryResultList) {
                wRoutePartPoint.ID = StringUtils.parseInt(wSqlDataReader.get("ID"));
                wRoutePartPoint.Name = StringUtils.parseString(wSqlDataReader.get("Name"));
                wRoutePartPoint.Code = StringUtils.parseString(wSqlDataReader.get("Code"));
                wRoutePartPoint.PartPointCode = StringUtils.parseString(wSqlDataReader.get("PartPointCode"));

                wRoutePartPoint.PartPointType = StringUtils.parseInt(wSqlDataReader.get("PartPointType"));
                wRoutePartPoint.RoutePartID = StringUtils.parseInt(wSqlDataReader.get("RoutePartID"));
                wRoutePartPoint.CreatorID = StringUtils.parseInt(wSqlDataReader.get("CreatorID"));
                wRoutePartPoint.CreateTime = StringUtils.parseCalendar(wSqlDataReader.get("CreateTime"));
                wRoutePartPoint.EditorID = StringUtils.parseInt(wSqlDataReader.get("EditorID"));
                wRoutePartPoint.EditTime = StringUtils.parseCalendar(wSqlDataReader.get("EditTime"));
                wRoutePartPoint.Active = StringUtils.parseInt(wSqlDataReader.get("Active"));

            }

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("FPC_CheckRoutePartPoint", ex);
        }
        return wRoutePartPoint;
    }

    public void DeleteList(BMSEmployee wLoginUser, List<FPCRoutePartPoint> wFPCRoutePartPointDelete,
                           OutResult<Integer> wErrorCode) {
        List<String> wResult = new ArrayList<String>();
        try {
            if (wFPCRoutePartPointDelete == null || wFPCRoutePartPointDelete.size() <= 0) {
                return;
            }

            List<Integer> wIDList =
                    wFPCRoutePartPointDelete.stream().map(p -> p.ID).distinct().collect(Collectors.toList());

            String wSQL = StringUtils.Format("DELETE FROM {0}.`fpc_routepartpoint` WHERE ID IN ({1});",
                    DBSchemas.PDM.getDBName(), StringUtils.Join(",", wIDList));

            Map<String, Object> wParamMap = new HashMap<String, Object>();

            wSQL = this.DMLChange(wSQL);

            this.update(wSQL, wParamMap);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
    }

    public void DeleteBOMItemList(BMSEmployee wLoginUser, List<MSSBOMItem> wMSSBOMItemDelete,
                                  OutResult<Integer> wErrorCode) {
        List<String> wResult = new ArrayList<String>();
        try {
            if (wMSSBOMItemDelete == null || wMSSBOMItemDelete.size() <= 0) {
                return;
            }

            List<Integer> wIDList = wMSSBOMItemDelete.stream().map(p -> p.ID).distinct().collect(Collectors.toList());

            String wSQL = StringUtils.Format("DELETE FROM {0}.`mss_bomitem` WHERE ID IN ({1});",
                    DBSchemas.PDM.getDBName(), StringUtils.Join(",", wIDList));

            Map<String, Object> wParamMap = new HashMap<String, Object>();

            wSQL = this.DMLChange(wSQL);

            this.update(wSQL, wParamMap);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
    }

    public void DeleteMeasureToolList(BMSEmployee wLoginUser, List<FPCRouteMeasureTool> wFPCRouteMeasureToolDelete,
                                      OutResult<Integer> wErrorCode) {
        List<String> wResult = new ArrayList<String>();
        try {
            if (wFPCRouteMeasureToolDelete == null || wFPCRouteMeasureToolDelete.size() <= 0) {
                return;
            }

            List<Integer> wIDList =
                    wFPCRouteMeasureToolDelete.stream().map(p -> p.ID).distinct().collect(Collectors.toList());

            String wSQL = StringUtils.Format("DELETE FROM {0}.`fpc_routemeasuretool` WHERE ID IN ({1});",
                    DBSchemas.PDM.getDBName(), StringUtils.Join(",", wIDList));

            Map<String, Object> wParamMap = new HashMap<String, Object>();

            wSQL = this.DMLChange(wSQL);

            this.update(wSQL, wParamMap);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
    }

    public void DeleteFrockToolList(BMSEmployee wLoginUser, List<FPCRouteFrockTool> wFPCRouteFrockToolDelete,
                                    OutResult<Integer> wErrorCode) {
        List<String> wResult = new ArrayList<String>();
        try {
            if (wFPCRouteFrockToolDelete == null || wFPCRouteFrockToolDelete.size() <= 0) {
                return;
            }

            List<Integer> wIDList =
                    wFPCRouteFrockToolDelete.stream().map(p -> p.ID).distinct().collect(Collectors.toList());

            String wSQL = StringUtils.Format("DELETE FROM {0}.`fpc_routefrocktool` WHERE ID IN ({1});",
                    DBSchemas.PDM.getDBName(), StringUtils.Join(",", wIDList));

            Map<String, Object> wParamMap = new HashMap<String, Object>();

            wSQL = this.DMLChange(wSQL);

            this.update(wSQL, wParamMap);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
    }
}
